package iscript.uiFramework.components.menu
{
	import com.greensock.easing.Linear;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import iscript.uiFramework.components.button.ButtonState;
	import iscript.uiFramework.components.button.BaseButton;
	import iscript.uiFramework.components.container.Container;
	import iscript.uiFramework.components.layout.FlowLayout;
	import iscript.uiFramework.components.layout.HBoxLayout;
	import iscript.uiFramework.components.layout.LayoutAlign;
	import iscript.uiFramework.components.layout.LayoutMargin;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.LineSegment;
	import iscript.uiFramework.util.UIComponentUtil;
	import org.libspark.swfassist.swf.structures.Rect;
	
	//INFO: 一级菜单
	//TODO: 允许多级菜单，加入其它组建的面板支持
	public class MenuStrip extends UIComponent
	{
		
		private var _menuItemList:Vector.<BaseButton>
		private var _hbox:Container;
		
		/**
		 * Item ToolStripMenuPanel
		 * @param	text
		 * @param	menuPanel
		 */
		private var _toolStripMenuPanelGroup:Dictionary;
		
		/**
		 * 当前操作的BaseButton
		 */
		private var _currentControlBaseButton:BaseButton;
		
		public function MenuStrip(preferWidth:Number=200,preferHeight:Number=30)
		{
			super(preferWidth, preferHeight);
		}
		
		override protected function initialize():void
		{
			_menuItemList = new Vector.<BaseButton>();
			_toolStripMenuPanelGroup = new Dictionary();
			_hbox = new Container(_preferWidth, _preferHeight);
			_hbox.setLayout(new FlowLayout());
			_hbox.getLayout().margin = new LayoutMargin(5,2,2,2);
			_hbox.render(this);			
			super.initialize();
		}
		
		override protected function eventListener():void 
		{
			UIFramework.UiStage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
			super.eventListener();
		}
		
		private function _mouseDown(e:MouseEvent):void
		{
			var item:ToolStripMenuPanel;
			var inSideMenuPanel:Boolean;
			var point:Point = new Point(UIFramework.UiStage.mouseX, UIFramework.UiStage.mouseY);
			for each (item in _toolStripMenuPanelGroup)
			{
				var rect:Rectangle = new Rectangle(item.x, item.y, item.width, item.height);
				if (item.visible && rect.containsPoint(point))
				{
					inSideMenuPanel = true;
					break;
				}
			}
			
			//鼠标没有点在其中一个MenuPanel范围中，就隐藏所有MenuPanel
			if (!inSideMenuPanel && _currentControlBaseButton != null)
			{
				if (!_currentControlBaseButton.hitMouse())
					_currentControlBaseButton.state = ButtonState.NORMAL;
				for each (item in _toolStripMenuPanelGroup)
				{
					item.visible = false;
				}
				
			}
		}
		
		override public function set width(value:Number):void
		{
			_hbox.width = value;
			if (IsRender)
				_hbox.getLayout().updateAlign();
			super.width = value;
		}
		
		override public function set height(value:Number):void
		{
			_hbox.height = value;
			if (IsRender)
				_hbox.getLayout().updateAlign();
			super.height = value;
		}
		
		override public function get width():Number 
		{
			return _hbox.width;
		}
		
		override public function get height():Number 
		{
			return _hbox.height;
		}
		
		public function addMenuItem(text:String, menuPanel:ToolStripMenuPanel = null):void
		{
			var button:BaseButton = new BaseButton();
			button.toggle = true;
			button.flat = true;
			button.text = text;
			button.theme = "MenuTheme";
			button.ClickHandler = _showTooStripMenuPanelLogic;
			button.HoverHandler = _menuItemButtonHoverLogic;
			button.render(this);
			
			if (menuPanel != null)
			{
				_toolStripMenuPanelGroup[button] = menuPanel
				menuPanel.visible = false;
				menuPanel.control = button;
				menuPanel.render();
			}
			_hbox.append(button);
			_menuItemList.push(button);
		}
	 
			
		private function _showTooStripMenuPanelLogic(sender:BaseButton):void
		{
			_currentControlBaseButton = sender;
			if (_toolStripMenuPanelGroup[sender] != null)
			{
				
			 	_toolStripMenuPanelGroup[sender].visible = sender.IsPressed;
				var pos:Point = UIComponentUtil.toStagePoint(sender);
				
				//确定X轴MenuPanel的位置在屏幕中
				_toolStripMenuPanelGroup[sender].x = pos.x;
				if (_toolStripMenuPanelGroup[sender].x < 0)
				{
					_toolStripMenuPanelGroup[sender].x = 0;
				}
				if ((_toolStripMenuPanelGroup[sender].x + _toolStripMenuPanelGroup[sender].width) > UIFramework.UiStage.stageWidth)
				{
					_toolStripMenuPanelGroup[sender].x = UIFramework.UiStage.stageWidth - _toolStripMenuPanelGroup[sender].width;
				}
				
				//确定Y轴MenuPanel的位置在屏幕中
				_toolStripMenuPanelGroup[sender].y = pos.y + sender.height - 1;
				if ((_toolStripMenuPanelGroup[sender].y + _toolStripMenuPanelGroup[sender].height) > UIFramework.UiStage.stageHeight)
				{
					_toolStripMenuPanelGroup[sender].y = pos.y - _toolStripMenuPanelGroup[sender].height + 4;
				}
				
				Stage(_toolStripMenuPanelGroup[sender].stage).setChildIndex(_toolStripMenuPanelGroup[sender],Stage(_toolStripMenuPanelGroup[sender].stage).numChildren-1)
				
			}
		}
		
		override public function render(container:DisplayObjectContainer = null):void
		{
			_hbox.getLayout().updateAlign(LayoutAlign.HAlign_CENTER);
			
			this.background = true;
			this.getBackground().begFillColor(0xEFEFEF);
			//this.getBackground().begFill(UIFramework.resource.getBitmapData("GameLite.UIComponent." + this.theme + ".Toolbar.Background"));
			this.border = true;
			super.render(container);
		}
		
		override public function dispose():Boolean 
		{
			if (super.dispose()) {
				UIFramework.UiStage.removeEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
				}
			return true;
		}
		
		
		
		/**
		 * 移动到其他菜单项,隐藏上次的MenuPanel
		 * @param	target
		 */
		private function _menuItemButtonHoverLogic(target:BaseButton):void
		{
			for each (var button:BaseButton in _menuItemList)
			{
				if (target != button)
				{
					button.state = ButtonState.NORMAL;
					if (ToolStripMenuPanel(_toolStripMenuPanelGroup[button]))
					{
						ToolStripMenuPanel(_toolStripMenuPanelGroup[button]).visible = false;
					}
				}
			}
		    target.setReleased();
		}
	
	}

}